package com.sailfish.springbootdemo.service.db1;

import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.configs.UserHeaderHolder;
import com.sailfish.springbootdemo.dao.db1.PcbaDeviceParamDao;
import com.sailfish.springbootdemo.dao.db1.PcbaDeviceRelatedDeviceParamDao;
import com.sailfish.springbootdemo.dao.db1.PcbaDeviceResourceDao;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db1.PcbaDeviceParam;
import com.sailfish.springbootdemo.pojo.db1.PcbaDeviceRelatedDeviceParam;
import com.sailfish.springbootdemo.pojo.db1.PcbaDeviceResource;
import com.sailfish.springbootdemo.pojo.db4.Operate;
import com.sailfish.springbootdemo.service.db4.OperateService;
import com.sailfish.springbootdemo.utils.CommonUtils;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PcbaDeviceRelatedDeviceParamServiceImpl implements PcbaDeviceRelatedDeviceParamService {

    @Autowired
    private PcbaDeviceRelatedDeviceParamDao pcbaDeviceRelatedDeviceParamDao;

    @Autowired
    private PcbaDeviceParamDao pcbaDeviceParamDao;

    @Autowired
    private PcbaDeviceResourceDao pcbaDeviceResourceDao;

    @Autowired
    private CoreAlgorithmService coreAlgorithmService;

    @Qualifier("MysqlTransactionManager1")
    @Autowired
    private DataSourceTransactionManager transactionManager1;

    @Autowired
    private OperateService operateService;

    private String operateSmallType = "基线资源池";

    @Override
    /*public List<PcbaDeviceRelatedDeviceParam> getAllByDeviceResourceId(Integer pcbaDeviceResourceId) {
        List<PcbaDeviceRelatedDeviceParam> list = pcbaDeviceRelatedDeviceParamDao.getAllByDeviceResourceId(pcbaDeviceResourceId);
        for (PcbaDeviceRelatedDeviceParam p : list) {
            PcbaDeviceParam searchPcbaDeviceParam = new PcbaDeviceParam();
            searchPcbaDeviceParam.setPcbaDeviceParamKey(p.getPcbaDeviceParamKey());
            searchPcbaDeviceParam.setPcbaDeviceTypeId(p.getPcbaDeviceTypeId());
            PcbaDeviceParam byPcbaDeviceParam = pcbaDeviceParamDao.getByPcbaDeviceParam(searchPcbaDeviceParam);
            if(byPcbaDeviceParam != null){
                p.setPcbaDeviceParam(byPcbaDeviceParam);
            }
        }
        return list;
    }*/

    public List<PcbaDeviceRelatedDeviceParam> getAllByDeviceResourceId(Integer pcbaDeviceResourceId) {
        List<PcbaDeviceRelatedDeviceParam> list = new ArrayList<>();
        PcbaDeviceResource deviceResource = pcbaDeviceResourceDao.getById(pcbaDeviceResourceId);
        if (deviceResource != null) {
            Integer pcbaDeviceTypeId = deviceResource.getPcbaDeviceTypeId();
            List<PcbaDeviceParam> allPcbaDeviceParam = pcbaDeviceParamDao.getAllPcbaDeviceParam(pcbaDeviceTypeId, null, null);
            for (PcbaDeviceParam pcbaDeviceParam : allPcbaDeviceParam) {
                PcbaDeviceRelatedDeviceParam pcbaDeviceRelatedDeviceParam = pcbaDeviceRelatedDeviceParamDao.getPcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId(pcbaDeviceParam.getPcbaDeviceParamKey(), pcbaDeviceResourceId);
                if (pcbaDeviceRelatedDeviceParam != null) {
                    pcbaDeviceRelatedDeviceParam.setPcbaDeviceParam(pcbaDeviceParam);
                    list.add(pcbaDeviceRelatedDeviceParam);
                } else {
                    PcbaDeviceRelatedDeviceParam p = new PcbaDeviceRelatedDeviceParam();
                    p.setPcbaDeviceParam(pcbaDeviceParam);
                    p.setPcbaDeviceParamKey(pcbaDeviceParam.getPcbaDeviceParamKey());
                    p.setPcbaDeviceParamValue("");
                    p.setPcbaDeviceResourceId(pcbaDeviceResourceId);
                    p.setPcbaDeviceTypeId(pcbaDeviceTypeId);
                    list.add(p);
                }
            }
        }
        return list;


//        List<PcbaDeviceRelatedDeviceParam> list = pcbaDeviceRelatedDeviceParamDao.getAllByDeviceResourceId(pcbaDeviceResourceId);
//        for (PcbaDeviceRelatedDeviceParam p : list) {
//            PcbaDeviceParam searchPcbaDeviceParam = new PcbaDeviceParam();
//            searchPcbaDeviceParam.setPcbaDeviceParamKey(p.getPcbaDeviceParamKey());
//            searchPcbaDeviceParam.setPcbaDeviceTypeId(p.getPcbaDeviceTypeId());
//            PcbaDeviceParam byPcbaDeviceParam = pcbaDeviceParamDao.getByPcbaDeviceParam(searchPcbaDeviceParam);
//            if(byPcbaDeviceParam != null){
//                p.setPcbaDeviceParam(byPcbaDeviceParam);
//            }
//        }
//        return list;
    }

    @Override
    public Result addOrUpdateDeviceRelatedParam(PcbaDeviceRelatedDeviceParam pcbaDeviceRelatedDeviceParam, String mode) {
        String userId = UserHeaderHolder.getUserId();
        Date d = new Date();
        try {
            if (pcbaDeviceRelatedDeviceParam == null) {
                return ResultUtil.error(500, "缺少参数pcbaDeviceRelatedDeviceParam", "Miss Param pcbaDeviceRelatedDeviceParam", null);
            }
            if (mode == null || "".equals(mode)) {
                return ResultUtil.error(500, "缺少参数mode", "Miss Param mode", null);
            }
            if (!("update".equals(mode) || "add".equals(mode))) {
                return ResultUtil.error(500, "mode 参数错误", "mode Param Error", null);
            }

            if ("update".equals(mode)) {
                if (pcbaDeviceRelatedDeviceParam.getPcbaDeviceRelatedDeviceParamId() == null || "".equals(pcbaDeviceRelatedDeviceParam.getPcbaDeviceRelatedDeviceParamId().toString())) {
                    return ResultUtil.error(500, "缺少参数pcbaDeviceRelatedDeviceParamId", "Miss Param pcbaDeviceRelatedDeviceParamId", null);
                }
                PcbaDeviceRelatedDeviceParam oldPcbaDeviceRelatedDeviceParam = pcbaDeviceRelatedDeviceParamDao.getPcbaDeviceRelatedDeviceParamById(pcbaDeviceRelatedDeviceParam.getPcbaDeviceRelatedDeviceParamId());
                if (updateDeviceRelatedParam(pcbaDeviceRelatedDeviceParam)) {
                    // 操作日志
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 3, 1, d, JSONObject.toJSONString(oldPcbaDeviceRelatedDeviceParam), JSONObject.toJSONString(pcbaDeviceRelatedDeviceParam)));
                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            } else {
                if (addDeviceRelatedParam(pcbaDeviceRelatedDeviceParam)) {
                    // 操作日志
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 1, 1, d, null, JSONObject.toJSONString(pcbaDeviceRelatedDeviceParam)));
                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Boolean addDeviceRelatedParam(PcbaDeviceRelatedDeviceParam pcbaDeviceRelatedDeviceParam) {
        pcbaDeviceRelatedDeviceParam.setCreateTime(new Date());
        return pcbaDeviceRelatedDeviceParamDao.addDeviceRelatedParam(pcbaDeviceRelatedDeviceParam) > 0;
    }

    @Override
    public Boolean updateDeviceRelatedParam(PcbaDeviceRelatedDeviceParam pcbaDeviceRelatedDeviceParam) {
        pcbaDeviceRelatedDeviceParam.setUpdateTime(new Date());
        return pcbaDeviceRelatedDeviceParamDao.updateDeviceRelatedParam(pcbaDeviceRelatedDeviceParam) > 0;
    }

    @Override
    public Result getPcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId(String pcbaDeviceParamkey, Integer pcbaDeviceResourceId) {
        try {
            if (pcbaDeviceParamkey == null || "".equals(pcbaDeviceParamkey)) {
                return ResultUtil.error(500, "缺少参数pcbaDeviceParamkey", "Miss Param pcbaDeviceParamkey", null);
            }
            if (pcbaDeviceResourceId == null || "".equals(pcbaDeviceResourceId.toString())) {
                return ResultUtil.error(500, "缺少参数pcbaDeviceResourceId", "Miss Param pcbaDeviceResourceId", null);
            }
            return ResultUtil.success(pcbaDeviceRelatedDeviceParamDao.getPcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId(pcbaDeviceParamkey, pcbaDeviceResourceId));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result getMinimumValueFromBaseKeys(String bindDeviceParamKey, Integer pcbaDeviceResourceId) {
        try {
            if (bindDeviceParamKey == null || "".equals(bindDeviceParamKey)) {
                return ResultUtil.error(500, "缺少参数bindDeviceParamKey", "Miss Param bindDeviceParamKey", null);
            }
            if (pcbaDeviceResourceId == null || "".equals(pcbaDeviceResourceId.toString())) {
                return ResultUtil.error(500, "缺少参数pcbaDeviceResourceId", "Miss Param pcbaDeviceResourceId", null);
            }

            String[] keyArr = bindDeviceParamKey.split(",");
            double value = 0;
            for (String s : keyArr) {
                PcbaDeviceRelatedDeviceParam pcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId = pcbaDeviceRelatedDeviceParamDao.getPcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId(s, pcbaDeviceResourceId);
                if (pcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId != null && !"".equals(pcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId.getPcbaDeviceParamValue())) {
                    if (CommonUtils.isNumeric(pcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId.getPcbaDeviceParamValue())) {
                        double v = Double.parseDouble(pcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId.getPcbaDeviceParamValue());
                        if (value == 0) {
                            value = v;
                        } else {
                            if (value >= v) {
                                value = v;
                            }
                        }
                    }
                }
            }
            return ResultUtil.success(value);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }
}
